<template>
  <div class="notes-app">
    <!-- 左侧文件夹列表 -->
    <div class="sidebar">
      <div class="sidebar-header">
        <h2>文件夹</h2>
        <button class="icon-btn" @click="showNewFolderDialog = true" title="新建文件夹">
          <span>➕</span>
        </button>
      </div>

      <div class="folder-list">
        <div
          v-for="folder in notesStore.foldersWithCount"
          :key="folder.id"
          class="folder-item"
          :class="{ active: notesStore.selectedFolderId === folder.id }"
          @click="notesStore.selectFolder(folder.id)"
        >
          <span class="folder-icon">{{ folder.icon }}</span>
          <span class="folder-name">{{ folder.name }}</span>
          <span class="folder-count">{{ folder.count }}</span>
        </div>
      </div>
    </div>

    <!-- 中间备忘录列表 -->
    <div class="notes-list">
      <div class="notes-list-header">
        <div class="search-box">
          <span class="search-icon">🔍</span>
          <input
            v-model="notesStore.searchQuery"
            type="text"
            placeholder="搜索备忘录"
            class="search-input"
          />
        </div>
        <button class="new-note-btn" @click="handleCreateNote" title="新建备忘录">
          <span>✏️</span>
          <span>新建备忘录</span>
        </button>
      </div>

      <div class="notes-scroll">
        <div
          v-for="note in notesStore.currentFolderNotes"
          :key="note.id"
          class="note-item"
          :class="{ active: notesStore.selectedNoteId === note.id, pinned: note.isPinned }"
          @click="notesStore.selectNote(note.id)"
        >
          <div class="note-item-header">
            <h3 class="note-title">{{ note.title || '新建备忘录' }}</h3>
            <span v-if="note.isPinned" class="pin-icon" title="已固定">📌</span>
            <span v-if="note.isLocked" class="lock-icon" title="已锁定">🔒</span>
          </div>
          <div class="note-preview">{{ getPreview(note.content) }}</div>
          <div class="note-meta">{{ formatDate(note.updatedAt) }}</div>
        </div>

        <div v-if="notesStore.currentFolderNotes.length === 0" class="empty-state">
          <p>📝</p>
          <p>暂无备忘录</p>
        </div>
      </div>
    </div>

    <!-- 右侧编辑器 -->
    <div class="editor-panel" v-if="notesStore.selectedNote">
      <div class="editor-header">
        <div class="editor-actions">
          <button
            class="action-btn"
            @click="notesStore.togglePin(notesStore.selectedNote.id)"
            :title="notesStore.selectedNote.isPinned ? '取消固定' : '固定'"
          >
            {{ notesStore.selectedNote.isPinned ? '📌' : '📍' }}
          </button>
          <button
            class="action-btn"
            @click="notesStore.toggleLock(notesStore.selectedNote.id)"
            :title="notesStore.selectedNote.isLocked ? '解锁' : '锁定'"
          >
            {{ notesStore.selectedNote.isLocked ? '🔒' : '🔓' }}
          </button>
          <button
            class="action-btn delete"
            @click="handleDeleteNote"
            title="删除"
          >
            🗑️
          </button>
        </div>
      </div>

      <div class="editor-content">
        <input
          v-model="notesStore.selectedNote.title"
          type="text"
          class="note-title-input"
          placeholder="标题"
          @input="handleTitleChange"
        />
        <textarea
          v-model="notesStore.selectedNote.content"
          class="note-content-textarea"
          placeholder="开始输入..."
          @input="handleContentChange"
        ></textarea>
      </div>
    </div>

    <div class="editor-panel empty" v-else>
      <div class="empty-editor">
        <p>📝</p>
        <p>选择一个备忘录或创建新备忘录</p>
      </div>
    </div>

    <!-- 新建文件夹对话框 -->
    <transition name="dialog-fade">
      <div v-if="showNewFolderDialog" class="dialog-overlay" @click="showNewFolderDialog = false">
        <div class="dialog" @click.stop>
          <div class="dialog-header">
            <h3>新建文件夹</h3>
          </div>
          <div class="dialog-body">
            <div class="form-group">
              <label>文件夹名称</label>
              <input
                v-model="newFolderName"
                type="text"
                class="form-input"
                placeholder="输入文件夹名称"
                @keyup.enter="handleCreateFolder"
              />
            </div>
            <div class="form-group">
              <label>选择图标</label>
              <div class="icon-picker">
                <button
                  v-for="icon in folderIcons"
                  :key="icon"
                  class="icon-option"
                  :class="{ selected: newFolderIcon === icon }"
                  @click="newFolderIcon = icon"
                >
                  {{ icon }}
                </button>
              </div>
            </div>
          </div>
          <div class="dialog-footer">
            <button class="btn-secondary" @click="showNewFolderDialog = false">取消</button>
            <button class="btn-primary" @click="handleCreateFolder">创建</button>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useNotesStore } from '@/stores/notes.ts'

const notesStore = useNotesStore()

const showNewFolderDialog = ref(false)
const newFolderName = ref('')
const newFolderIcon = ref('📁')

const folderIcons = ['📁', '📂', '📋', '📌', '📍', '💼', '🏠', '🎨', '🎵', '🎬', '📚', '💡', '🎯', '🔥', '⭐', '❤️']

function getPreview(content: string): string {
  // 去除Markdown标记，获取纯文本预览
  const text = content
    .replace(/^#+\s+/gm, '') // 标题
    .replace(/\*\*(.+?)\*\*/g, '$1') // 粗体
    .replace(/\*(.+?)\*/g, '$1') // 斜体
    .replace(/\[(.+?)\]\(.+?\)/g, '$1') // 链接
    .replace(/^[-*+]\s+/gm, '') // 列表
    .replace(/\n/g, ' ') // 换行
    .trim()

  return text.length > 100 ? text.substring(0, 100) + '...' : text
}

function formatDate(date: Date): string {
  const now = new Date()
  const diff = now.getTime() - date.getTime()
  const days = Math.floor(diff / (1000 * 60 * 60 * 24))

  if (days === 0) {
    const hours = Math.floor(diff / (1000 * 60 * 60))
    if (hours === 0) {
      const minutes = Math.floor(diff / (1000 * 60))
      return minutes <= 1 ? '刚刚' : `${minutes} 分钟前`
    }
    return `${hours} 小时前`
  } else if (days === 1) {
    return '昨天'
  } else if (days < 7) {
    return `${days} 天前`
  } else {
    return date.toLocaleDateString('zh-CN', { month: 'long', day: 'numeric' })
  }
}

function handleCreateNote() {
  notesStore.createNote()
}

function handleTitleChange() {
  if (notesStore.selectedNote) {
    notesStore.updateNote(notesStore.selectedNote.id, {
      title: notesStore.selectedNote.title
    })
  }
}

function handleContentChange() {
  if (notesStore.selectedNote) {
    notesStore.updateNote(notesStore.selectedNote.id, {
      content: notesStore.selectedNote.content
    })
  }
}

function handleDeleteNote() {
  if (notesStore.selectedNote && confirm('确定要删除这个备忘录吗？')) {
    notesStore.deleteNote(notesStore.selectedNote.id)
  }
}

function handleCreateFolder() {
  if (newFolderName.value.trim()) {
    notesStore.createFolder(newFolderName.value.trim(), newFolderIcon.value)
    newFolderName.value = ''
    newFolderIcon.value = '📁'
    showNewFolderDialog.value = false
  }
}
</script>

<style scoped>
.notes-app {
  display: flex;
  height: 100%;
  background: #f5f5f7;
  overflow: hidden;
}

/* 左侧文件夹列表 */
.sidebar {
  width: 200px;
  background: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(40px);
  border-right: 1px solid rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  padding-top: 40px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.08);
}

.sidebar-header h2 {
  font-size: 18px;
  font-weight: 700;
  color: #1d1d1f;
  margin: 0;
}

.icon-btn {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  border: none;
  background: transparent;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  transition: background 0.2s;
}

.icon-btn:hover {
  background: rgba(0, 0, 0, 0.05);
}

.folder-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.folder-item {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.2s;
  margin-bottom: 2px;
}

.folder-item:hover {
  background: rgba(0, 0, 0, 0.05);
}

.folder-item.active {
  background: rgba(0, 122, 255, 0.1);
}

.folder-icon {
  font-size: 16px;
  margin-right: 8px;
}

.folder-name {
  flex: 1;
  font-size: 14px;
  color: #1d1d1f;
  font-weight: 500;
}

.folder-count {
  font-size: 12px;
  color: #86868b;
  font-weight: 600;
}

/* 中间备忘录列表 */
.notes-list {
  width: 300px;
  background: rgba(242, 242, 247, 0.8);
  backdrop-filter: blur(40px);
  border-right: 1px solid rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
}

.notes-list-header {
  padding: 16px;
  padding-top: 40px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.08);
}

.search-box {
  position: relative;
  margin-bottom: 12px;
}

.search-icon {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 14px;
  opacity: 0.5;
}

.search-input {
  width: 100%;
  padding: 8px 12px 8px 36px;
  border-radius: 8px;
  border: 0.5px solid rgba(0, 0, 0, 0.12);
  background: rgba(255, 255, 255, 0.6);
  font-size: 13px;
  outline: none;
  box-shadow:
    0 1px 2px rgba(0, 0, 0, 0.08),
    0 0 0 0.5px rgba(255, 255, 255, 0.5) inset;
  transition:
    border-color 0.25s cubic-bezier(0.4, 0, 0.2, 1),
    box-shadow 0.25s cubic-bezier(0.4, 0, 0.2, 1),
    background-color 0.2s cubic-bezier(0.4, 0, 0.2, 1),
    transform 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

.search-input:hover:not(:focus) {
  background: rgba(255, 255, 255, 0.75);
  border-color: rgba(0, 0, 0, 0.2);
  box-shadow:
    0 2px 4px rgba(0, 0, 0, 0.1),
    0 0 0 0.5px rgba(255, 255, 255, 0.6) inset;
}

.search-input:focus {
  border-color: #007AFF;
  background: rgba(255, 255, 255, 0.95);
  box-shadow:
    0 0 0 3.5px rgba(0, 122, 255, 0.15),
    0 2px 6px rgba(0, 0, 0, 0.1),
    0 0 0 0.5px #007AFF inset;
  transform: translateY(-1px);
}

.new-note-btn {
  width: 100%;
  padding: 10px;
  border-radius: 8px;
  border: none;
  background: #FFD60A;
  color: #1d1d1f;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all 0.2s;
}

.new-note-btn:hover {
  background: #FF9F0A;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(255, 214, 10, 0.3);
}

.notes-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.note-item {
  padding: 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.2s;
  margin-bottom: 4px;
  background: white;
  border: 1px solid transparent;
}

.note-item:hover {
  background: #f9f9f9;
}

.note-item.active {
  background: white;
  border-color: #007AFF;
  box-shadow: 0 2px 8px rgba(0, 122, 255, 0.15);
}

.note-item.pinned {
  background: #fffbeb;
}

.note-item-header {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 6px;
}

.note-title {
  flex: 1;
  font-size: 15px;
  font-weight: 600;
  color: #1d1d1f;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pin-icon,
.lock-icon {
  font-size: 12px;
  opacity: 0.6;
}

.note-preview {
  font-size: 13px;
  color: #86868b;
  line-height: 1.4;
  margin-bottom: 6px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.note-meta {
  font-size: 11px;
  color: #aeaeb2;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #86868b;
}

.empty-state p:first-child {
  font-size: 48px;
  margin: 0 0 12px 0;
}

.empty-state p:last-child {
  font-size: 14px;
  margin: 0;
}

/* 右侧编辑器 */
.editor-panel {
  flex: 1;
  background: white;
  display: flex;
  flex-direction: column;
}

.editor-panel.empty {
  display: flex;
  align-items: center;
  justify-content: center;
}

.empty-editor {
  text-align: center;
  color: #86868b;
}

.empty-editor p:first-child {
  font-size: 64px;
  margin: 0 0 16px 0;
}

.empty-editor p:last-child {
  font-size: 16px;
  margin: 0;
}

.editor-header {
  padding: 16px 20px;
  padding-top: 40px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.08);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.editor-actions {
  display: flex;
  gap: 8px;
}

.action-btn {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  border: none;
  background: rgba(0, 0, 0, 0.03);
  cursor: pointer;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

.action-btn:hover {
  background: rgba(0, 0, 0, 0.08);
}

.action-btn.delete:hover {
  background: rgba(255, 59, 48, 0.1);
}

.editor-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 20px;
  overflow: hidden;
}

.note-title-input {
  width: 100%;
  padding: 12px 0;
  border: none;
  font-size: 28px;
  font-weight: 700;
  color: #1d1d1f;
  outline: none;
  margin-bottom: 16px;
  background: transparent;
}

.note-title-input::placeholder {
  color: #c7c7cc;
}

.note-content-textarea {
  flex: 1;
  width: 100%;
  padding: 0;
  border: none;
  font-size: 16px;
  line-height: 1.6;
  color: #1d1d1f;
  outline: none;
  resize: none;
  font-family: inherit;
  background: transparent;
}

.note-content-textarea::placeholder {
  color: #c7c7cc;
}

/* 对话框 */
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.dialog {
  background: white;
  border-radius: 12px;
  width: 400px;
  max-width: 90%;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.2);
}

.dialog-header {
  padding: 20px 24px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.08);
}

.dialog-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #1d1d1f;
}

.dialog-body {
  padding: 24px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group:last-child {
  margin-bottom: 0;
}

.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: #1d1d1f;
  margin-bottom: 8px;
}

.form-input {
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid rgba(0, 0, 0, 0.15);
  background: rgba(0, 0, 0, 0.02);
  font-size: 14px;
  color: #1d1d1f;
  outline: none;
  transition: all 0.2s;
}

.form-input:focus {
  border-color: #007AFF;
  box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.1);
}

.icon-picker {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 8px;
}

.icon-option {
  aspect-ratio: 1;
  border-radius: 8px;
  border: 2px solid transparent;
  background: rgba(0, 0, 0, 0.03);
  cursor: pointer;
  font-size: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

.icon-option:hover {
  background: rgba(0, 0, 0, 0.08);
}

.icon-option.selected {
  border-color: #007AFF;
  background: rgba(0, 122, 255, 0.1);
}

.dialog-footer {
  padding: 16px 24px;
  border-top: 1px solid rgba(0, 0, 0, 0.08);
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

.btn-secondary,
.btn-primary {
  padding: 8px 20px;
  border-radius: 8px;
  border: none;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-secondary {
  background: rgba(0, 0, 0, 0.05);
  color: #1d1d1f;
}

.btn-secondary:hover {
  background: rgba(0, 0, 0, 0.1);
}

.btn-primary {
  background: #007AFF;
  color: white;
}

.btn-primary:hover {
  background: #0051D5;
}

/* 对话框动画 */
.dialog-fade-enter-active,
.dialog-fade-leave-active {
  transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

.dialog-fade-enter-from,
.dialog-fade-leave-to {
  opacity: 0;
}

.dialog-fade-enter-from .dialog,
.dialog-fade-leave-to .dialog {
  transform: scale(0.95);
}

/* 深色模式 */
html.dark-mode .notes-app {
  background: #1c1c1e;
}

html.dark-mode .sidebar {
  background: rgba(28, 28, 30, 0.8);
  border-right-color: rgba(255, 255, 255, 0.1);
}

html.dark-mode .sidebar-header h2,
html.dark-mode .folder-name,
html.dark-mode .note-title,
html.dark-mode .note-title-input,
html.dark-mode .note-content-textarea {
  color: #f5f5f7;
}

html.dark-mode .notes-list {
  background: rgba(44, 44, 46, 0.8);
  border-right-color: rgba(255, 255, 255, 0.1);
}

html.dark-mode .search-input,
html.dark-mode .note-item {
  background: rgba(58, 58, 60, 0.8);
  border-color: rgba(255, 255, 255, 0.1);
  color: #f5f5f7;
}

html.dark-mode .editor-panel {
  background: #1c1c1e;
}

html.dark-mode .dialog {
  background: #2c2c2e;
}

html.dark-mode .dialog-header h3,
html.dark-mode .form-group label {
  color: #f5f5f7;
}

html.dark-mode .form-input {
  background: rgba(255, 255, 255, 0.05);
  border-color: rgba(255, 255, 255, 0.1);
  color: #f5f5f7;
}

html.dark-mode .icon-option {
  background: rgba(255, 255, 255, 0.05);
}

/* 滚动条样式 */
.folder-list::-webkit-scrollbar,
.notes-scroll::-webkit-scrollbar {
  width: 6px;
}

.folder-list::-webkit-scrollbar-track,
.notes-scroll::-webkit-scrollbar-track {
  background: transparent;
}

.folder-list::-webkit-scrollbar-thumb,
.notes-scroll::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.15);
  border-radius: 3px;
}

html.dark-mode .folder-list::-webkit-scrollbar-thumb,
html.dark-mode .notes-scroll::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.15);
}
</style>

